package com.nf.sms.controller;

import com.nf.sms.dao.UserDao;
import com.nf.sms.dao.impl.UserDaoImpl;
import com.nf.sms.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * 实现用户注册
 *
 * @author ZQX
 * @date 2024-10-10
 */
@WebServlet("/reg.do")
public class RegServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 第一：获取表单中的数据
        String name = req.getParameter("uname");
        String pass = req.getParameter("upass");
        String phone = req.getParameter("uphone");
        String mail = req.getParameter("umail");

        // 1.实例化对象
        User user = new User();

        // 2.初始化对象
        user.setUsername(name);
        user.setPassword(pass);
        user.setPhone(phone);
        user.setEmail(mail);

        // 第二：实例化 Dao 对象
        UserDao userDao = new UserDaoImpl();

        // 第三：调用 Dao对象中的 insert
        int row = userDao.insert(user);

        // 第三：逻辑判断，是否存在用户
        if (row == 1) {
            resp.sendRedirect("reg_success.jsp");
            return ;
        }

        req.setAttribute("msg","注册失败,重新<a href='reg.html'>注册</a>");
        req.getRequestDispatcher("error.jsp").forward(req,resp);
    }
}
